package com.example.studentmanager.service.impl;

import com.example.studentmanager.mapper.CourseMapper;
import com.example.studentmanager.model.Course;
import com.example.studentmanager.model.vo.CourseVo;
import com.example.studentmanager.model.vo.GradeVo;
import com.example.studentmanager.service.CourseService;
import com.example.studentmanager.utils.BaseDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;


@Component
@Service
public class CourseServiceImpl extends Thread implements CourseService {

    @Autowired
    Course course;




    @Autowired
    CourseMapper courseMapper;
    SqlSession sqlSession= new BaseDao().getSession();


    List<Course> courseList;





    public CourseServiceImpl(){}



    @Override
    public void addCourse(Course course) {
        courseMapper=sqlSession.getMapper(CourseMapper.class);
        try {
            courseMapper.insertCourse(course);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCourse(int courseid) {
        courseMapper=sqlSession.getMapper(CourseMapper.class);
        try {
            courseMapper.deleteCourse(courseid);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public int searchCourseNum(Course course) {
        courseMapper=sqlSession.getMapper(CourseMapper.class);
        int courseNum=courseMapper.selectCourseNum(course);
        return courseNum;
    }

    @Override
    public List<CourseVo> searchAllCourse(Integer page, Integer rows, String searchCol, String searchValue) {
        Integer start=(page-1)*rows;
        List<CourseVo> courses=courseMapper.selectAllCourse(start,rows,searchCol,searchValue);
        if(courses!=null)
            return courses;
        else
            throw new RuntimeException("分数未查询到");
    }

    @Override
    public Integer counts(String searchCol, String searchValue) {
        return courseMapper.counts(searchCol, searchValue);
    }

    @Override
    public void addStuCourse(int sid, int cid) {
        try {
            courseMapper.insertStuCourse(sid,cid);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void cancelCourse(int sid, int cid) {
        courseMapper.deleteStuCourse(sid,cid);
        sqlSession.commit();

    }

    @Override
    public void changeCourse(Course course) {
        try {
            courseMapper.updateStatus(course);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
/*
    public void run(){
        //chooseCourse.setCourse(course);
        //int a=course.getCredithour();
        int a=10;
        System.out.println("生产者");
      // courseList.add(this.course);
        chooseCourse.setCourse(searchAllCourse());
        //course.setCredithour(++a);

    }*/
}
